<div class="main-content">
    <div class="section__content section__content--p20">
        <div class="container-fluid">
            <div class="row m-b-30">
                <div class="col-lg-12">
                    <div class="overview-wrap">
                        <h2 class="title-1"><?php echo $id == 0 ? "新建用户" : "用户编辑" ?></h2>
                    </div>
                </div>
            </div>
            <div class="row">
                <div class="col-lg-12">
                    <div class="card">
                        <div class="card-header">
                            <strong><?php echo $id == 0 ? "新建用户" : "用户编辑" ?></strong>
                        </div>
                        <div class="card-body">
                            <form class="form-horizontal" role="form" name="form" id="form" method="post"
                                  data-bv-feedbackicons-valid="glyphicon glyphicon-ok"
                                  data-bv-feedbackicons-invalid="glyphicon glyphicon-remove"
                                  data-bv-feedbackicons-validating="glyphicon glyphicon-refresh"
                                  action="index.php?r=admin/user/edit">
                                <input type="hidden" value="<?php echo $userinfo['user_id']; ?>" name="user_id"/>
                                <input type="hidden" name="YII_CSRF_TOKEN" value="<?php echo Yii::app()->request->csrfToken; ?>"/>
                                <input type="hidden" name="isadmin" value="1">

                                <div class="form-group">
                                    <label class=" form-control-label">登入名称</label>
                                    <?php
                                    if ($id != 0) {
                                        ?>
                                        <input type="text" name="loginname" class="form-control readonly" readonly="readonly"
                                               autocomplete="off"
                                               value="<?php echo $userinfo['loginname']; ?>" data-bv-notempty="true"
                                               data-bv-notempty-message="名称是必须的！"/>
                                        <?php
                                    } else {
                                        ?>
                                        <input type="text" name="loginname" class="form-control validate[required] " autocomplete="off"
                                               value="<?php echo $userinfo['loginname']; ?>" data-bv-notempty="true"
                                               data-bv-notempty-message="名称是必须的！"/>
                                        <?php
                                    }
                                    ?>
                                </div>

                                <?php
                                if ($id == 0) {
                                    ?>
                                    <div class="form-group">
                                        <label class=" form-control-label">登入密码</label>
                                        <input type="password" name="loginpass" id="loginpass" autocomplete="off"
                                               class="form-control validate[required] "/>
                                    </div>

                                    <div class="form-group">
                                        <label class=" form-control-label">确认密码</label>
                                        <input type="password" name="loginpass_confirm" autocomplete="off"
                                               class="form-control validate[required,equals[loginpass]]  "/>
                                    </div>
                                    <?php
                                }
                                ?>


                                <div class="form-group">
                                    <label class=" form-control-label">用户名</label>
                                    <?php
                                    if ($id != 0){
                                        ?>
                                        <input type="text" value="<?php echo $userinfo['username']; ?>" class="form-control" name="username"
                                               readonly="readonly"/></td>
                                        <?php
                                    } else {
                                        ?>
                                        <input type="text" class="form-control validate[required] " name="username"/></td>
                                        <?php
                                    }
                                    ?>
                                </div>

                                <div class="form-group">
                                    <label class=" form-control-label">所属群组</label>
                                    <select class="form-control" name="group_id">
                                        <?php
                                        foreach ($groups as $g) {
                                            if ($g['id'] == $userinfo['group_id']) {
                                                ?>
                                                <option value="<?php echo $g['id']; ?>"
                                                        selected="selected"><?php echo $g['groupname']; ?></option>
                                                <?php
                                            } else {
                                                ?>
                                                <option
                                                        value="<?php echo $g['id']; ?>"><?php echo $g['groupname']; ?></option>
                                                <?php
                                            }
                                        }
                                        ?>
                                    </select>
                                </div>

                                <div class="form-group">
                                    <label class=" form-control-label">性别</label>
                                    <div>
                                        <?php
                                        if ($userinfo['sex'] == 0) {
                                            ?>
                                            <input type="radio" value="1" name="sex"/>男&nbsp;&nbsp;<input type="radio" value="0"
                                                                                                          name="sex"
                                                                                                          checked="checked"/>女
                                            <?php
                                        } else {
                                            ?>
                                            <input type="radio" value="1" name="sex" checked="checked"/>男&nbsp;&nbsp;<input type="radio"
                                                                                                                            value="0"
                                                                                                                            name="sex"/>女
                                            <?php
                                        }
                                        ?>
                                    </div>
                                </div>

                                <div class="form-group">
                                    <label class=" form-control-label">电话</label>
                                    <input type="text" class="form-control validate[required] " name="tel"
                                           value="<?php echo $userinfo['tel'] ?>"/>
                                </div>

                                <div class="form-group">
                                    <label class=" form-control-label">手机</label>
                                    <input type="text" class="form-control validate[required] " name="phone"
                                           value="<?php echo $userinfo['phone'] ?>"/>
                                </div>

                                <div class="form-group">
                                    <label class=" form-control-label">Email</label>
                                    <input type="text" class="form-control validate[required,custom[email]] " name="email"
                                           value="<?php echo $userinfo['email'] ?>"/>
                                </div>

                                <div class="form-group">
                                    <label class=" form-control-label">激活</label>
                                    <label class="control-label">
                                        <?php
                                        if ($userinfo['isvalid'] == 1) {
                                            ?>
                                            <input type="checkbox" name="isvalid" value="1" checked="checked"/>
                                            <?php
                                        } else {
                                            ?>
                                            <input type="checkbox" name="isvalid" value="1"/>
                                            <?php
                                        }
                                        ?>
                                    </label>
                                </div>

                                <div class="form-group">
                                    <label class=" form-control-label">角色</label>
                                    <select class="form-control" name="role_id">
                                        <?php
                                        foreach ($roles as $r) {
                                            if ($userinfo['role_id'] == $r['id']) {
                                                ?>
                                                <option selected="selected"
                                                        value="<?php echo $r['id']; ?>"><?php echo $r['rolename']; ?></option>
                                                <?php
                                            } else {
                                                ?>
                                                <option value="<?php echo $r['id']; ?>"><?php echo $r['rolename']; ?></option>
                                                <?php
                                            }
                                        }
                                        ?>
                                    </select>
                                </div>

                                <div class="form-group">
                                    <button  type="submit" name="submit" id="submit" class="btn btn-primary btn-lg btn-block">保存</button>
                                </div>

                            </form>
                        </div>
                    </div>
                </div>
            </div>
        </div>
    </div>
</div>
<script>
    $(document).ready(function () {
        initPage();
    });
    function initPage() {
        $('#form').bootstrapValidator();
        $('#form').ajaxForm({
            dataType: 'json',
            beforeSubmit: function (formData, jqForm, options) {
                return $('#form').data('bootstrapValidator').isValid();
            },
            success: function (res) {
                if (message(res)) {
                    window.location.href = "index.php?r=admin/user/index";
                }
            }
        });
    }
</script>